// **********************
// PI 2008 - Symbios
// **********************

//**********************************************************
//************   DETECTION DE CHAIR (DC) ******************
//**********************************************************

//includes
#include <cv.h>
#include <highgui.h>
#include <stdio.h>

//DC
int dc() {

cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "mywindow2", CV_WINDOW_AUTOSIZE );

//Capture CAM
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture ) {
    fprintf( stderr, "ERROR: capture is NULL \n" );
    getchar();
    return -1;
    }

//Acquisition dun modele de chair
IplImage* modele = cvQueryFrame( capture );
while( 1 ) {
	modele = cvQueryFrame( capture );
	if( !modele ) {
		fprintf( stderr, "ERROR: frame is null...\n" );
		getchar();
		break;
		}
     	cvShowImage( "mywindow2", modele );
     	if( (cvWaitKey(10) & 255) == 27 ) break;
	}
modele = cvQueryFrame( capture );
cvShowImage( "mywindow2", modele );


//Recuperation de 3 canaux RGB
IplImage* hsv = cvCreateImage( cvGetSize(modele), 8, 3 );
cvCvtColor( modele, hsv, CV_BGR2HSV );
IplImage* h_plane = cvCreateImage( cvGetSize(modele), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(modele), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(modele), 8, 1 );
IplImage* planes[] = { h_plane, s_plane, v_plane };
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );

//Creation de l'histogramme
int h_bins = 10, s_bins = 10, v_bins = 10;
CvHistogram* hist;
{
	int     hist_size[] = { h_bins, s_bins, v_bins};
	float h_ranges[] = { 0, 180 };                // hue is [0,180]
	float s_ranges[] = { 0, 255 };
	float v_ranges[] = { 0, 255};
	float* ranges[]  = { h_ranges, s_ranges, v_ranges};
	hist = cvCreateHist(3,hist_size, CV_HIST_ARRAY,ranges, 1);
}
cvCalcHist( planes, hist, 0, 0 );
//cvNormalizeHist( hist, 255 ); //Normalize it


//on attend pour continuer
cvWaitKey(0);
//fin de la phase de calibrage



//Detection de chair
IplImage* frame;
IplImage* result;
IplImage* h_plane2;
IplImage* s_plane2;
IplImage* v_plane2;
IplImage* src2;
IplImage* hsv2;
IplImage* result2;

while( 1 ) {
	//Recuperer l image courante
	frame = cvQueryFrame( capture );
	if( !frame ) {
		fprintf( stderr, "ERROR: frame is null...\n" );
		getchar();
		break;
		}
	//Traitement de limage (idem plus haut)
	src2 = cvCloneImage(frame);
/*
	cvAbsDiff( frame, frame_fond, frame);
	cvThreshold(frame,frame,20,255,CV_THRESH_BINARY);
	result2 =  cvCreateImage( cvGetSize(src2), 8 , 1 );
	cvCvtColor(frame,result2,CV_RGB2GRAY);
	cvThreshold(result2,result2,128,255,CV_THRESH_BINARY);
	cvShowImage("mywindow3",result2);
*/
        hsv2 = cvCreateImage( cvGetSize(src2), 8, 3 );
 	cvCvtColor( src2, hsv2, CV_BGR2HSV );
        h_plane2 = cvCreateImage( cvGetSize(src2), 8, 1 );
        s_plane2 = cvCreateImage( cvGetSize(src2), 8, 1 );
        v_plane2 = cvCreateImage( cvGetSize(src2), 8, 1 );
	IplImage* planes2[] = { h_plane2, s_plane2, v_plane2};
        cvCvtPixToPlane( hsv2, h_plane2, s_plane2, v_plane2, 0 );
	result =  cvCreateImage( cvGetSize(src2), 8 , 1 );
	//Extraction des zones chairs
	cvCalcBackProject( planes2, result, hist);
	cvThreshold( result,result, 125, 255, CV_THRESH_BINARY_INV);
	cvSmooth(result,result, CV_MEDIAN,3,3,0,0);
	//Affichage image
	cvShowImage( "mywindow", result );
	cvShowImage( "mywindow2", frame );
	if( (cvWaitKey(10) & 255) == 27 ) break;
}


//Desalocation ressource
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
cvDestroyWindow( "mywindow2" );
cvReleaseImage(&hsv);
cvReleaseImage(&src2);
cvReleaseImage(&hsv2);
cvReleaseImage(&h_plane);
cvReleaseImage(&v_plane);
cvReleaseImage(&s_plane);
cvReleaseImage(&h_plane2);
cvReleaseImage(&v_plane2);
cvReleaseImage(&s_plane2);
cvReleaseImage(&result2);

return 1;
}
